// pages/pot/pot.js
Page({
  data: {
    plants: [],
    originalPlants: [],   // 存储原始数据的副本
    searchKeyword: '',
    showPopup: false,
    selectedPlant: null
  },
  onLoad: function () {
    this.fetchPlants();
  },
  fetchPlants: function () {
    wx.request({
      url: 'http://120.26.231.14:8080/plant/all', 
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === '1') {
          const processedPlants = res.data.data.map(plant => ({
            id:plant.id,
            name:plant.name,
            imgUrl: plant.imgUrl,
            description:plant.careInstructions
          }));
          console.log('Processed Plants:', processedPlants); // 打印处理后的数据
          this.setData({
            plants: processedPlants,
            originalPlants: processedPlants.slice()
          });
        } else {
          console.error('Failed to fetch plants:', res.statusCode, res.data);
        }
      },
      fail: (err) => {
        console.error('Request failed:', err);
      }
    });
  },
  onShow: function () {},
  onSearchInput: function (event) {
    const keyword = event.detail.value.trim().toLowerCase();
    this.setData({
      searchKeyword: keyword
    });
    if (keyword.length > 0) {
      this.searchPlants(keyword);
    } else {
      // 重置显示的数据集合，使其恢复为原始数据
      this.setData({
        plants: this.data.originalPlants
      });
      // this.forceUpdate();
    }
  },
  searchPlants: function (keyword) {
    wx.request({
      url: 'http://120.26.231.14:8080/plant?pageSize=100&search=' + keyword, 
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === '1') {
          const processedPlants = res.data.data.records.map(plant => ({
            id:plant.id,
            name:plant.name,
            imgUrl: plant.imgUrl,
            description:plant.careInstructions
          }));
          this.setData({
            plants: processedPlants
          });
          console.log('Filtered Plants:', this.data.plants);
        } else {
          console.error('Failed to search plants:', res.statusCode, res.data);
        }
      },
      fail: (err) => {
        console.error('Request failed:', err);
      }
    });
  },
  selectPlant: function (e) {
    console.log('Selected Plant:', e.currentTarget.dataset.id); // 检查是否能进入此方法
    const plantId = e.currentTarget.dataset.id;
    const selectedPlant = this.data.plants.find(plant => plant.id === plantId);
    if (selectedPlant) {
      this.setData({
        selectedPlant: selectedPlant,
        showPopup: true
      });
      console.log('Selected Plant Details:', selectedPlant); // 检查 selectedPlant
      console.log('showPopup:', this.data.showPopup); // 检查 showPopup
    }
  },
  closePopup: function () {
    this.setData({
      showPopup: false,
      selectedPlant: null
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})